app.controller('shopLocatedEditCtrl', ['$scope', 'utilService', 'shopLocatedEditService', '$modal', '$stateParams', 'toaster', '$state',
    function ($scope, utilService, shopLocatedEditService, $modal, $stateParams, toaster, $state) {
        //定义交互对象
        $scope.shopLocated = {};
        //交互对象副本
        $scope.shopLocatedCopy = {};
        //定义套餐对象
        $scope.setMealList = [];
        //定义店铺对象
        $scope.shopList = [];
        //定义联想查询
        $scope.states = [];
        //取得参数
        $scope.id = $stateParams.id;

        //初始化调用函数
        $scope.init = function () {
            //根据ID查询入驻信息
            $scope.findById();
            //查询套餐信息
            $scope.queryMeal();
            //查询店铺信息
            $scope.queryShop();
        }

        //查询店铺信息
        $scope.queryShop = function () {
            shopLocatedEditService.queryShop($scope).then(function (result) {
                $scope.shopList = result.data;
            });
        }
        //查询套餐信息
        $scope.queryMeal = function () {
            shopLocatedEditService.obtainedMeal($scope).then(function (result) {
                $scope.setMealList = result.data;
            });
        }
        //根据ID查询入住信息
        $scope.findById = function () {
            shopLocatedEditService.findById($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        $scope.shopLocated = result.data;
                        angular.extend($scope.shopLocatedCopy, result.data);
                        break;
                    default:
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }

        //联想查询请求函数 
        $scope.getLocation = function (val) {
            return utilService.get({
                url: $scope.app.domain + 'user',
                params: {name:val}
            }).then(function (response) {
                $scope.states = response.data.list;
                return response.data.list;
            }, function (e) {
                deferred.reject("连接服务器出错！");
            });
        };

        //提交
        $scope.submit = function () {
            //如果当前推荐用户在请求数组中就找到该信息并把code赋值给该推荐用户的code信息
            /**
             * 1、选择好推荐用户信息后，又对其进行了操作导致查找不到对应的code无法修改
             * 2、选择联想查询的时候可能存在网络延时导致用户无法修改
             * 3、没有匹配到对应的用户导致无法修改
             */
            if($scope.shopLocated.recommendUserName == ''){
                $scope.shopLocated.userCode = null;
            }
            for (var key in $scope.states) {
                if($scope.states[key].name  == $scope.shopLocated.recommendUserName){
                    $scope.shopLocated.userCode = $scope.states[key].code;
                }
            }
            if (shopLocatedEditService.comparing($scope.shopLocatedCopy, $scope.shopLocated)) {
                shopLocatedEditService.submit($scope).then(function (result) {
                    if (result) {
                        toaster.pop('success', "提示", "操作成功");
                        $state.go('app.shopLocated');
                    } else {
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    }
                });
            } else {
                toaster.pop('error', "操作失败", "没有做任何修改~");
            }
        }
    }
]);
